<?php
class Cp_PhotoAlbumsController extends Pidguin_Controller_Cp_Action
{
	public function indexAction()
	{
		$this->view->albums = Model_PhotoAlbums::getInstance()->fetchAll();
	}
	
	public function createAction()
	{
		$this->view->groups = Model_Groups::getInstance()->fetchAll();
	}
	
	public function createSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
		
		$album = Model_PhotoAlbums::getInstance()->createRow();
		$album->title = $this->_getParam('title');
		$album->description = $this->_getParam('description');
		$album->created = date('Y-m-d H:i:s');
		$album->save();
		
		$photos = $this->_getParam('photos');
		
		if (count($photos) > 0) {
			$_am = Model_Attachments::getInstance();
			foreach ($photos as $photo) {
				if (!preg_match('/[0-9]*/', $photo['photo_id'])) {
					continue;
				}
				$_ar = $_am->fetchRow($_am->select()->from('attachments', array('id', 'description'))
					->where('id = ?', $photo['photo_id']));
				if (!$_ar) {
					continue;
				}
				if (!empty($photo['photo_description'])) {
					$_ar->description = $photo['photo_description'];
					$_ar->save();
				}
				$afnr = Model_AttachmentsForAlbums::getInstance()->createRow();
				$afnr->album_id = $album->id;
				$afnr->attachment_id = $photo['photo_id'];
				$afnr->save();
			}
		}
		
		$this->_helper->json(array('status' => 'ok'));
	}
	
	public function deleteAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Album id is not set');
		}
	
		$m = Model_PhotoAlbums::getInstance();
		$select = $m->select()->from('photo_albums', array('id'))
			->where(Model_PhotoAlbums::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Photo album not found');
		}
		$row->delete();
		$this->_redirect('/cp/photo.albums');
	}
	
	public function editAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Album id is not set');
		}
		
		$m = Model_PhotoAlbums::getInstance();
		$select = $m->select()->from('photo_albums', array('id', 'title', 'description'))
			->where(Model_PhotoAlbums::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Photo album not found');
		}
		
		$_photos = $row->findDependentRowset('Model_AttachmentsForAlbums');
	    $photos = array();
	    if (count($_photos)) {
	        foreach ($_photos as $photo) {
	            $_a = $photo->findParentRow('Model_Attachments');
	            $photos[] = array('url' => $_a->url, 'id' => $_a->id, 'description' => $_a->description);
	        }
	    }
		
		$this->view->album = $row;
		$this->view->photos = Zend_Json::encode($photos);
	}
	
	public function editSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Photo album ID is not set');
		}
		
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
		
		// find album
		$m = Model_PhotoAlbums::getInstance();
		$select = $m->select()->from('photo_albums', array('id', 'title', 'description', 'modified'))
			->where(Model_PhotoAlbums::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$albumRow = $m->fetchRow($select);
		if (!$albumRow) {
			throw new Zend_Controller_Action_Exception('Photo album not found');
		}
		$albumRow->title = $this->_getParam('title');
		$albumRow->description = $this->_getParam('description');
		$albumRow->modified = date('Y-m-d H:i:s');
		$albumRow->save();
		
		$photos = $this->_getParam('photos');
		
		if (count($photos) > 0) {
			$_am = Model_Attachments::getInstance();
			foreach ($photos as $photo) {
				if (!preg_match('/[0-9]*/', $photo['photo_id'])) {
					continue;
				}
				$_ar = $_am->fetchRow($_am->select()->from('attachments', array('id', 'description'))
					->where('id = ?', $photo['photo_id']));
				if (!$_ar) {
					continue;
				}
				if (!empty($photo['photo_description'])) {
					$_ar->description = $photo['photo_description'];
					$_ar->save();
				}
				$j = $_ar->findDependentRowset('Model_AttachmentsForAlbums', 'Attachment');
				if (count($j) > 0) continue;
				$afnr = Model_AttachmentsForAlbums::getInstance()->createRow();
				$afnr->album_id = $albumRow->id;
				$afnr->attachment_id = $photo['photo_id'];
				$afnr->save();
			}
		}
		
		$this->_helper->json(array('status' => 'ok'));
	}
}